package com.cyz.user.runner;

import com.cyz.common.bean.Card;
import com.cyz.user.userService.CardService;
import com.cyz.common.utils.MyRedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class ViewCountRunner implements CommandLineRunner {

    @Autowired
    private CardService cardService;
    @Autowired
    private MyRedisUtil myRedisUtil;

    @Override
    public void run(String... args){
        List<Card> list = cardService.list();
        Map<String, Object> collect = list.stream()
                .collect(Collectors.toMap(
                        item -> item.getId().toString(),Card::getViewCount
                ));
//        redisUtil.setCacheMap("card:viewCount",collect);
//        redisUtil.myHashPutAll("card:viewCount",collect);
        myRedisUtil.myHashPutAll("card:viewCount",collect);
    }
}
